package com.yangguang.huahui.ui.fragments;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.Request;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.SizeReadyCallback;
import com.bumptech.glide.request.target.Target;
import com.yangguang.huahui.R;
import com.yangguang.huahui.entity.DataInfo;
import com.yangguang.huahui.utils.ImageLoader;
import com.yangguang.huahui.utils.LogUtil;
import com.yangguang.huahui.utils.QRCodeUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/4/13.
 */

public class MyQRCodeFragment extends BaseFragment {
    @BindView(R.id.mLL_show_code)
    LinearLayout mLL_show_code;
    @BindView(R.id.iv_qrcode)
    ImageView iv_qrcode;
    @BindView(R.id.raiv_head)
    ImageView raiv_head;
    @BindView(R.id.tv_user_name)
    TextView tv_user_name;
    private Bitmap codeBitmap;

    @Override
    protected int initLayoutId() {
        return R.layout.frag_myqrcode;
    }

    @Override
    protected void initView() {
        if (getArguments().getByteArray("byte[]") != null) {
            logoBit = BitmapFactory.decodeByteArray(getArguments().getByteArray("byte[]"), 0, getArguments().getByteArray("byte[]").length);
        }else {
            logoBit = BitmapFactory.decodeResource(getResources(), R.mipmap.head);
        }

        creareQRCode();
        ImageLoader.loadCircle(mActivity, DataInfo.AVATARS, raiv_head);
        tv_user_name.setText(DataInfo.NICKNAME);

    }

    @Override
    protected void initData() {

    }

    InputStream is;
    Bitmap logoBit;
    private String code_str = "";

    private void creareQRCode() {
        code_str = "http://a.app.qq.com/o/simple.jsp?pkgname=com.workers.wuyou&token=" + DataInfo.TOKEN;
        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                DisplayMetrics metrics = new DisplayMetrics();
                mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
                int qrcodeWidth = (int) (metrics.widthPixels * 0.7);
                codeBitmap = QRCodeUtil.createQRImage(code_str,
                        qrcodeWidth, qrcodeWidth,
                        logoBit);
                if (codeBitmap != null) {
                    iv_qrcode.setImageBitmap(codeBitmap);
                }
            }
        });
    }

}
